package org.nightrunner.d3.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.nightrunner.d3.dao.ICTEDao;
import org.nightrunner.d3.entity.CTE;
import org.nightrunner.d3.entity.Category;
import org.nightrunner.d3.entity.Equipment;
import org.nightrunner.d3.model.DefaultPaginationInfo;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.ICTEService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * CTE Service implementation
 * 
 * @author NigthRunner
 * @date 2012-7-26
 * @version 1.0
 */

@Service
public class CTEService implements ICTEService {

	@Autowired
	private ICTEDao cteDao;

	public ICTEDao getCTEDao() {
		return cteDao;
	}

	public void setCTEDao(ICTEDao cteDao) {
		this.cteDao = cteDao;
	}

	public Serializable addCTE(CTE cte) {
		return cteDao.save(cte);
	}

	public CTE getCTE(String bureauId) {
		return cteDao.get(bureauId);
	}

	public List<CTE> getAllCTE() {
		return cteDao.list();
	}

	public void deleteCTE(CTE cte) {
		cteDao.delete(cte);
	}

	public PaginationModel<List<CTE>> getCTE() {
		IPaginationInfo paginationInfo = new DefaultPaginationInfo();
		paginationInfo.setCurrentPage(0);
		paginationInfo.setRowSize(10);
		return getCTE(paginationInfo);
	}

	public PaginationModel<List<CTE>> getCTE(IPaginationInfo pagination) {
		return cteDao.find(pagination);
	}

	public void updateCTE(CTE cte) {
		cteDao.merge(cte);
	}

	@Override
	public List<Category> getCategorys(Equipment equipment) {
		List<CTE> ctes = equipment.getCTEs();
		List<Category> categorys = new ArrayList<Category>();
		for (CTE cte : ctes) {
			categorys.add(cte.getCategory());
		}
		return categorys;
	}

	@Override
	public List<Equipment> getEquipments(Category category) {
		List<CTE> ctes = category.getCTEs();
		List<Equipment> matters = new ArrayList<Equipment>();
		for (CTE cte : ctes) {
			matters.add(cte.getEquipment());
		}
		return matters;
	}
}
